// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Windows.Forms;
using OpenNETCF.Windows.Forms;
using svs.Mobile.BeyondMedia.Controls;
using svs.Mobile.BeyondMedia.FeedCore;
using svs.Mobile.BeyondMedia.Properties;
using svs.Mobile.BeyondMedia.UiCore;

namespace svs.Mobile.BeyondMedia
{
	public partial class Categories : Form
	{
		private bool CategoriesChanged = false;

		public Categories()
		{
			InitializeComponent();
			CategoryView.CustomizedFor = KineticCategoryListView.CustomizeFor.CategoryManagement;
		}

		public static DialogResult ShowCategoryEditor()
		{
			return new Categories().ShowDialog();
		}

		private void MenuBack_Click(object sender, EventArgs e)
		{
			Close();
		}

		static bool CategoryValidator(string category)
		{
			return !string.IsNullOrEmpty(category) && CategoryManager.GetCategoryByValue(category) == CategoryManager.CategoryNull;
		}

		private void MenuNewCategory_Click(object sender, EventArgs e)
		{
			string catName = InputBoxEx.ShowInputBox("New Category","Category Name","",
				"Enter a name for this category (for example: News, Sport etc.)", CategoryValidator,
			"The category name is empty or already exists!","Invalid Category");

			if(!string.IsNullOrEmpty(catName))
			{
				try
				{
					CategoryManager.AddCategory(catName);
					LoadCategories();
					CategoriesChanged = true;
				}
				catch(Exception ex)
				{
					MessageBoxEx.Show(ex.Message);
				}
			}
		}

		private void MenuDeleteCategory_Click(object sender, EventArgs e)
		{
			if(CategoryView.SelectedCategory == null)
				return;

			if( MessageBoxEx.Show(string.Format(Resources.DeleteCategoryMsg, CategoryView.SelectedCategory.Name),Resources.DeleteCategoryTitle,MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
			{
				CategoryManager.DeleteCategory(CategoryView.SelectedCategory);
				LoadCategories();
				CategoriesChanged = true;
			}
		}

		private void MenuEditCategory_Click(object sender, EventArgs e)
		{
			if (CategoryView.SelectedCategory == null)
				return;

			string catName = InputBoxEx.ShowInputBox("Edit Category", "Category Name", CategoryView.SelectedCategory.Name,
															"Enter a name for this category (for example: News, Sport etc.)", CategoryValidator,
															"The category name is empty or already exists!", "Invalid Category");
			if (!string.IsNullOrEmpty(catName))
			{
				try
				{
					CategoryManager.RenameCategory(CategoryView.SelectedCategory, catName);
					LoadCategories();
					CategoriesChanged = true;
				}
				catch (Exception ex)
				{
					MessageBoxEx.Show(ex.Message);
				}
			}
		}

		private void OnLoad(object sender, EventArgs e)
		{
			LoadCategories();
		}

		private void LoadCategories()
		{
			CategoryView.SetCategories(CategoryManager.GetCategoriesForEdit(),null);
		}

		protected override void OnClosed(EventArgs e)
		{
			if (CategoriesChanged)
			{
				try
				{
					Cursor.Current = Cursors.WaitCursor;
					Configuration.SaveSettings();
					FeedRepository.SaveRepository();
				}
				finally
				{
					Cursor.Current = Cursors.Default;
				}
			}
		}
	}
}